// C11:MutexEvenGenerator.cpp {RunByHand}
//  Preventing thread collision with mutexes.
//  {L} ZThread
#include "EvenChecker.h"
#include "zthread/Mutex.h"
#include "zthread/ThreadedExecutor.h"
#include <iostream>

using namespace ZThread;
using namespace std;

class MutexEvenGenerator : public Generator {
  unsigned int currentEvenValue;
  Mutex lock;

public:
  MutexEvenGenerator() { currentEvenValue = 0; }
  ~MutexEvenGenerator() { cout << "~MutexEvenGenerator" << endl; }

  int nextValue() {
    lock.acquire();
    ++currentEvenValue;
    Thread::yield(); // Cause failure faster
    ++currentEvenValue;
    int rval = currentEvenValue;
    lock.release();
    return rval;
  }
};

int main(void) { EvenChecker::test<MutexEvenGenerator>(); }